package com.sc.study.ar.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.sc.common.utils.DateUtils;
import com.sc.system.service.ISysConfigService;
import org.apache.commons.collections4.CollectionUtils;
import com.sc.common.config.SCConfig;
import com.sc.common.utils.file.FileUploadUtils;
import com.sc.study.ar.domain.ViolationRegisterQuery;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sc.common.annotation.Log;
import com.sc.common.core.controller.BaseController;
import com.sc.common.core.domain.AjaxResult;
import com.sc.common.enums.BusinessType;
import com.sc.study.ar.domain.ScViolationRegister;
import com.sc.study.ar.service.IScViolationRegisterService;
import com.sc.common.utils.poi.ExcelUtil;
import com.sc.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 违纪登记Controller
 * 
 * @author sc
 * @date 2022-03-14
 */
@RestController
@RequestMapping("/study/violationregister")
public class ScViolationRegisterController extends BaseController
{
    @Autowired
    private IScViolationRegisterService scViolationRegisterService;

    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询违纪登记列表
     */
    @PreAuthorize("@ss.hasPermi('study:violationregister:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScViolationRegister scViolationRegister)
    {
        startPage();
        List<ScViolationRegister> list = scViolationRegisterService.selectScViolationRegisterList(scViolationRegister);
        return getDataTable(list);
    }

    /**
     * 导出违纪登记列表
     */
    @PreAuthorize("@ss.hasPermi('study:violationregister:export')")
    @Log(title = "违纪登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScViolationRegister scViolationRegister)
    {
        List<ScViolationRegister> list = scViolationRegisterService.selectScViolationRegisterList(scViolationRegister);
        ExcelUtil<ScViolationRegister> util = new ExcelUtil<ScViolationRegister>(ScViolationRegister.class);
        util.exportExcel(response, list, "违纪登记数据");
    }

    /**
     * 获取违纪登记详细信息
     */
    @PreAuthorize("@ss.hasPermi('study:violationregister:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(scViolationRegisterService.selectScViolationRegisterById(id));
    }

    /**
     * 新增违纪登记
     */
    @PreAuthorize("@ss.hasPermi('study:violationregister:add')")
    @Log(title = "违纪登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScViolationRegister scViolationRegister)
    {
        return toAjax(scViolationRegisterService.insertScViolationRegister(scViolationRegister));
    }


    /**
     * 批量新增违纪登记
     */
    @PreAuthorize("@ss.hasPermi('study:violationregister:add')")
    @Log(title = "违纪登记", businessType = BusinessType.INSERT)
    @PostMapping("/batchAdd")
    public AjaxResult batchAdd(ViolationRegisterQuery violationRegisterQuery, MultipartFile[] multipartFiles) throws IOException {
        StringBuffer sb = new StringBuffer();
        // 上传文件路径
        String filePath = SCConfig.getUploadPath();
        if (multipartFiles != null && multipartFiles.length > 0) {
            for (MultipartFile m : multipartFiles) {
                String fileName = FileUploadUtils.upload(filePath, m);
                sb.append(m.getOriginalFilename() + ",");
            }
        }
        violationRegisterQuery.setImages(sb.toString());

        return AjaxResult.success(scViolationRegisterService.batchAdd(violationRegisterQuery));
    }



    /**
     * 批量新增违纪登记(考勤)
     * @return
     */
    @PreAuthorize("@ss.hasPermi('study:violationregister:add')")
    @Log(title = "违纪登记", businessType = BusinessType.INSERT)
    @PostMapping("/batchAddCheckOnWork")
    public AjaxResult batchAdd(@RequestBody Map<String,Object> map) throws ParseException {
        List<Integer> integerList = (List<Integer>) map.get("studentIdList");
        List<Long> studentIdList=new ArrayList<>();
        Long violationTypeId = Long.valueOf(sysConfigService.selectConfigByKey("check_on_work"));
        CollectionUtils.collect(integerList,id->Long.valueOf(id.toString()),studentIdList);

        String violationRegisterContext = (String) map.get("violationRegisterContext");
        String violationTime = (String) map.get("violationtime");


        String TimeStart = violationTime.replace("Z", " UTC");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        Date callbackTimeStart =format.parse(TimeStart);

        ScViolationRegister violationRegister=new ScViolationRegister();
        violationRegister.setInfo(violationRegisterContext);

        violationRegister.setViolationtime(DateUtils.dateTime(callbackTimeStart));
        violationRegister.setViolationtypeid(violationTypeId);

        studentIdList.forEach(id->{
            violationRegister.setStudentid(id);
            scViolationRegisterService.insertScViolationRegister(violationRegister);
        });

        return toAjax(true);
    }


    /**
     * 修改违纪登记
     */
    @PreAuthorize("@ss.hasPermi('study:violationregister:edit')")
    @Log(title = "违纪登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScViolationRegister scViolationRegister)
    {
        return toAjax(scViolationRegisterService.updateScViolationRegister(scViolationRegister));
    }

    /**
     * 删除违纪登记
     */
    @PreAuthorize("@ss.hasPermi('study:violationregister:remove')")
    @Log(title = "违纪登记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scViolationRegisterService.deleteScViolationRegisterByIds(ids));
    }
}
